<?php
/**
 * Created by PhpStorm.
 * User: fengzi
 * Date: 2019/3/29
 * Time: 17:42
 */

namespace app\admin\controller;
use app\admin\logic\OperationLogic;
use app\common\model\Operation;
use think\facade\Request;

class Menu extends Base{

    private $operationLogic;

    public function __construct(){
        parent::__construct();
        $this->operationLogic = new OperationLogic();
    }

    /**
     * 菜单列表首页
     * @return mixed
     */
    public function index(){
        return $this->fetch();
    }

    /**
     * 菜单列表(异步获取菜单列表)
     * @return \think\response\Json
     */
    public function ajaxIndex(){
        $tempWhere = Request::post();
        $res = $this->operationLogic->menuList($tempWhere, self::$nowPage);
        return json($res);
    }

    /**
     * 编辑/添加菜单
     * @return mixed
     */
    public function add(){
        $id = Request::get('id');
        $act = 'add';
        $info = [];
        if ($id){
            $act = 'edit';
            $info = $this->operationLogic->info($id);
            $this->assign('info',$info['data']);
        }
        $parentList = $this->operationLogic->getChildMenu(0);
        $this->assign('parent_list',$parentList['data']);
        $this->assign('act', $act);
        return $this->fetch();
    }

    /**
     * 处理操作
     * @return \think\response\Json
     */
    public function handle(){
        $data = Request::param();
        $res = $this->operationLogic->handle($data);
        return json($res);
    }

    /**
     * 异步根据父菜单获取子菜单栏
     * @return \think\response\Json
     */
    public function getChildMenu(){
        $pId = Request::post('pid');
        $type = Request::post('type');
        $res = $this->operationLogic->getChildMenu($pId, $type);
        return json($res);
    }

}